package com.blb.community.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;

/**
 * @author hkp
 */
public class IpaddrUtil {
    public static String getIpAddress() throws IOException {
        Document document = Jsoup.connect("https://myip.ipip.net/").get();
        Element body = document.body();
        String text = body.text();
        String ipaddr = text.substring(text.indexOf("：") + 1, text.indexOf("来") - 1);
        return ipaddr;
    }

    public static String getLoginLocation() throws IOException {
        Document document = Jsoup.connect("https://myip.ipip.net/").get();
        Element body = document.body();
        String text = body.text();
        String location = text.substring(text.lastIndexOf("：") + 4, text.lastIndexOf(" "));
        String province = location.substring(0, location.indexOf(" "));
        String city = location.substring(location.indexOf(" ") + 1);
        province += "省 ";
        city += "市";
        String loginLocation = province + city;
        return loginLocation;
    }
}
